package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.DictUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.common.utils.StringUtils;
import com.huike.common.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Override
    public List<TbBusinessTrackRecord> getList(Long id) {
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordMapper.getList(id);

        for (TbBusinessTrackRecord tbBusinessTrackRecord : list) {
            //调用字符串工具类切割keyItems
            List<String> strNum = StringUtils.str2List(tbBusinessTrackRecord.getKeyItems(), ",", true, true);
            List<String> keys = new ArrayList<>();
            for (String s : strNum) {
                //调用字典工具类查询
                String communication_point = DictUtils.getDictLabel("communication_point", s);
                //将查到的沟通重点存到
                keys.add(communication_point);
            }
            tbBusinessTrackRecord.setKeys(keys);
        }

        return list;
    }

    /**
     * 对商务跟进表进行新增，对商务表进行修改
     *
     * @param businessTrackVo
     */
    @Override
    @Transactional //对多张表进行增删改事务管理
    public void add(BusinessTrackVo businessTrackVo) {
        //调用beanutils工具类将属性拷贝到TbBusinessTrackRecord实例中
        TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
        BeanUtils.copyBeanProp(tbBusinessTrackRecord, businessTrackVo);
        tbBusinessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        tbBusinessTrackRecord.setCreateTime(DateUtils.getNowDate());

        //新增
        tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);

        //修改商务表
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyBeanProp(tbBusiness, businessTrackVo);
        tbBusiness.setId(businessTrackVo.getBusinessId());
        tbBusinessMapper.updateTbBusiness(tbBusiness);

    }
}
